﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rad_Studio_Keygen
{
    public class RadLicense
    {
        public static string CreateLicenseText(string SerialNumber, string ActiveCode, RadStudioVersion RadStudioVersion)
        {
            var random = new Random();
            var rndkey = GetRndKey();
            var pid = RadStudioVersion.LicHostPID.ToString();
            var skuid = RadStudioVersion.LicHostSKU.ToString();
            var Result = "11\n";
            Result += "e.pkg\nEmbarcadero RAD Studio " + RadStudioVersion.LicVerStr + " Architect\n";
            Result += "e.pt\n10\n";
            Result += "e.sign\n0\n";
            Result += "e.sign2\n0\n";
            Result += "e.sign3\n0\n";
            Result += "export.allowed\n0\n";
            Result += "import.allowed\n1\n";
            Result += "import.silent\n1\n";
            Result += "licensed.serialno\n" + SerialNumber + "\n";
            Result += "nodelock.node\n0\n";
            Result += "nodelock.session\n" + ActiveCode + "\r\n";

            Result += "4\n";

            Result += "26\n";
            Result += "active\nT\n";
            Result += "beta\n0\n";
            Result += "exportable\n0\n";
            Result += "hostpid\n" + pid + "\n";
            Result += "hostskuid\n" + skuid + "\n";
            Result += "internaluse\n0\n";
            Result += "naggy\n0\n";
            Result += "noncommercial\n0\n";
            Result += "noncommercial_label\nNo\n";
            Result += "platform\n1\n";
            Result += "platform_label\nWindows\n";
            Result += "product\n2000\n";
            Result += "productid\n" + RadStudioVersion.LicDelphiPID + "\n";
            Result += "productid_label\nDelphi " + RadStudioVersion.LicVerStr + "\n";
            Result += "productsku\n" + skuid + "\n";
            Result += "productsku_label\nArchitect\n";
            Result += $"rndkey\n{rndkey}\n";
            Result += "serialno\n" + SerialNumber + "\n";
            Result += "sku\n" + skuid + "\n";
            Result += "templicense\n0\n";
            Result += "termtype\n0\n";
            Result += "termtype_label\nUnlimited\n";
            Result += "title\nDelphi " + RadStudioVersion.LicVerStr + " Architect\n";
            Result += "trial\n0\n";
            Result += "upgrade\n0\n";
            Result += "version\n27\n";
            Result += "27\n";
            Result += "Android\nT\n";
            Result += "DESIGNDIAGRAMS\nTRUE\n";
            Result += "DESIGNPROJECTS\nTRUE\n";
            Result += "Desktop\nT\n";
            Result += "FULLQA\nTRUE\n";
            Result += "FulliOS\nT\n";
            Result += "Linux64\nT\n";
            Result += "MODELLING\nTRUE\n";
            Result += "Mobile\nT\n";
            Result += "OSX32\nT\n";
            Result += "OSX64\nT\n";
            Result += "Win32\nT\n";
            Result += "Win64\nT\n";
            Result += "a100\nMakeThingsHappen\n";
            Result += "a1000\nPrintMoreMoney\n";
            Result += "a101\nImGivinItAllShesGot\n";
            Result += "a200\nStampIt\n";
            Result += "a250\nItsToolTimeBaby\n";
            Result += "a300\nTheMalteseFalcon\n";
            Result += "a301\nGlueSolvent\n";
            Result += "hostsuite\n" + pid + "\n";
            Result += "iOSDevice\nT\n";
            Result += "iOSDevice32\nT\n";
            Result += "iOSDevice64\nT\n";
            Result += "iOSSimulator\nT\n";
            Result += "updatelevel\n0.0\n";
            Result += "updates\n1\n";

            Result += "26\n";
            Result += "active\nT\n";
            Result += "beta\n0\n";
            Result += "exportable\n0\n";
            Result += "hostpid\n" + pid + "\n";
            Result += "hostskuid\n" + skuid + "\n";
            Result += "internaluse\n0\n";
            Result += "naggy\n0\n";
            Result += "noncommercial\n0\n";
            Result += "noncommercial_label\nNo\n";
            Result += "platform\n1\n";
            Result += "platform_label\nWindows\n";
            Result += "product\n4000\n";
            Result += "productid\n" + RadStudioVersion.LicCBuilderPID + "\n";
            Result += "productid_label\nC++Builder " + RadStudioVersion.LicVerStr + "\n";
            Result += "productsku\n" + skuid + "\n";
            Result += "productsku_label\nArchitect\n";               //Architect
            Result += $"rndkey\n{rndkey}\n";
            Result += "serialno\n" + SerialNumber + "\n";
            Result += "sku\n" + skuid + "\n";
            Result += "templicense\n0\n";
            Result += "termtype\n0\n";
            Result += "termtype_label\nUnlimited\n";
            Result += "title\nC++Builder " + RadStudioVersion.LicVerStr + " Architect\n";
            Result += "trial\n0\n";
            Result += "upgrade\n0\n";
            Result += "version\n20\n";
            Result += "25\n";
            Result += "Android\nT\n";
            Result += "DESIGNDIAGRAMS\nTRUE\n";
            Result += "DESIGNPROJECTS\nTRUE\n";
            Result += "Desktop\nT\n";
            Result += "FULLQA\nTRUE\n";
            Result += "FulliOS\nT\n";
            Result += "Linux64\nT\n";
            Result += "MODELLING\nTRUE\n";
            Result += "Mobile\nT\n";
            Result += "OSX32\nT\n";
            Result += "Win32\nT\n";
            Result += "Win64\nT\n";
            Result += "a100\nMakeThingsHappen\n";
            Result += "a1000\nPrintMoreMoney\n";
            Result += "a101\nImGivinItAllShesGot\n";
            Result += "a200\nStampIt\n";
            Result += "a250\nItsToolTimeBaby\n";
            Result += "a300\nTheMalteseFalcon\n";
            Result += "a301\nGlueSolvent\n";
            Result += "hostsuite\n" + pid + "\n";
            Result += "iOSDevice32\nT\n";
            Result += "iOSDevice64\nT\n";
            Result += "iOSSimulator\nT\n";
            Result += "updatelevel\n0.0\n";
            Result += "updates\n1\n";


            Result += "26\n";
            Result += "active\nT\n";
            Result += "beta\n0\n";
            Result += "exportable\n0\n";
            Result += "hostpid\n" + pid + "\n";
            Result += "hostskuid\n" + skuid + "\n";
            Result += "internaluse\n0\n";
            Result += "naggy\n0\n";
            Result += "noncommercial\n0\n";
            Result += "noncommercial_label\nNo\n";
            Result += "platform\n0\n";
            Result += "platform_label\nCross Platform\n";
            Result += "product\n7000\n";
            Result += "productid\n7113\n";
            Result += "productid_label\nInterBase 2020\n";        //InterBase XE7
            Result += "productsku\n0\n";
            Result += "productsku_label\nServer\n";               //Server
            Result += $"rndkey\n{rndkey}\n";
            Result += "serialno\n" + SerialNumber + "\n";
            Result += "sku\n0\n";
            Result += "templicense\n0\n";
            Result += "termtype\n0\n";
            Result += "termtype_label\nUnlimited\n";
            Result += "title\nInterBase 2020 Server\n";      //InterBase XE7 Server
            Result += "trial\n0\n";
            Result += "upgrade\n0\n";
            Result += "version\n8\n";
            Result += "21\n";
            Result += "changeView\n1\n";
            Result += "connectionMonitoring\n1\n";
            Result += "connectionsPerUser\n200\n";
            Result += "customVarId\n \n";
            Result += "databaseAccess\n1\n";
            Result += "dbEncryption\n1\n";
            Result += "ddlOperations\n1\n";
            Result += "devLicense\n1\n";
            Result += "externalFileAccess\n1\n";
            Result += "internetAccess\n1\n";
            Result += "languages\nALL\n";
            Result += "licensedCpus\n32\n";
            Result += "licensedUsers\n5000\n";
            Result += "nodeID\n \n";
            Result += "otwEncryption\n1\n";
            Result += "remoteAccess\n1\n";
            Result += "serverAccess\n1\n";
            Result += "togoAccess\n0\n";
            Result += "updatelevel\n0.0\n";
            Result += "useAddons\n0\n";
            Result += "version\n14.0\n";


            Result += "26\n";
            Result += "active\nT\n";
            Result += "beta\n0\n";
            Result += "exportable\n0\n";
            Result += "hostpid\n" + pid + "\n";
            Result += "hostskuid\n" + skuid + "\n";
            Result += "internaluse\n0\n";
            Result += "naggy\n0\n";
            Result += "noncommercial\n0\n";
            Result += "noncommercial_label\nNo\n";
            Result += "platform\n0\n";
            Result += "platform_label\nCross Platform\n";
            Result += "product\n7000\n";
            Result += "productid\n7113\n";
            Result += "productid_label\nInterBase 2020\n";        //InterBase XE7
            Result += "productsku\n16\n";
            Result += "productsku_label\nToGo Edition\n";               //ToGo Edition
            Result += $"rndkey\n{rndkey}\n";
            Result += "serialno\n" + SerialNumber + "\n";
            Result += "sku\n16\n";
            Result += "templicense\n0\n";
            Result += "termtype\n0\n";
            Result += "termtype_label\nUnlimited\n";
            Result += "title\nInterBase 2020 ToGo Edition\n";      //InterBase XE7 ToGo Edition
            Result += "trial\n0\n";
            Result += "upgrade\n0\n";
            Result += "version\n8\n";
            Result += "21\n";
            Result += "changeView\n1\n";
            Result += "connectionMonitoring\n1\n";
            Result += "connectionsPerUser\n200\n";
            Result += "customVarId\n \n";
            Result += "databaseAccess\n1\n";
            Result += "dbEncryption\n1\n";
            Result += "ddlOperations\n1\n";
            Result += "devLicense\n1\n";
            Result += "externalFileAccess\n1\n";
            Result += "internetAccess\n1\n";
            Result += "languages\nALL\n";
            Result += "licensedCpus\n32\n";
            Result += "licensedUsers\n5000\n";
            Result += "nodeID\n \n";
            Result += "otwEncryption\n1\n";
            Result += "remoteAccess\n1\n";
            Result += "serverAccess\n1\n";
            Result += "togoAccess\n1\n";
            Result += "updatelevel\n0.0\n";
            Result += "useAddons\n0\n";
            Result += "version\n14.0\n1\n";
            Result += "updatelevel\n0.0\n";

            //Result += "26\n";
            //Result += "active\nT\n";
            //Result += "beta\n0\n";
            //Result += "exportable\n0\n";
            //Result += "hostpid\n" + pid + "\n";
            //Result += "hostskuid\n" + skuid + "\n";
            //Result += "internaluse\n0\n";
            //Result += "naggy\n0\n";
            //Result += "noncommercial\n0\n";
            //Result += "noncommercial_label\nNo\n";
            //Result += "platform\n1\n";
            //Result += "platform_label\nWindows\n";
            //Result += "product\n14100\n";
            //Result += "productid\n14110\n";
            //Result += "productid_label\nER/Studio 2016\n";
            //Result += "productsku\n15\n";
            //Result += "productsku_label\nDeveloper MultiPlatform\n"; //Developer MultiPlatform
            //Result += "rndkey\n15687805\n";
            //Result += "serialno\n" + SerialNumber + "\n";
            //Result += "sku\n15\n";
            //Result += "templicense\n0\n";
            //Result += "termtype\n0\n";
            //Result += "termtype_label\nPermanent\n";
            //Result += "title\nER/Studio Developer 2016\n";
            //Result += "trial\n0\n";
            //Result += "upgrade\n0\n";
            //Result += "version\n10\n";
            //Result += "30\n";
            //Result += "CaErwin3Erx\nT\n";
            //Result += "CaErwin3ErxImport.MicrosoftOfficeVisio\nT\n";
            //Result += "CaErwin4Xml\nT\n";
            //Result += "CaErwin7ModelManager\nT\n";
            //Result += "CaErwin7Xml\nT\n";
            //Result += "CaErwin8ModelManager\nT\n";
            //Result += "CaErwin8Xml\nT\n";
            //Result += "CaErwin9Mart\nT\n";
            //Result += "CaErwin9Xml\nT\n";
            //Result += "CaMart9\nT\n";
            //Result += "CaModelManager7\nT\n";
            //Result += "CaModelManager8\nT\n";
            //Result += "CrossPlatform\nT\n";
            //Result += "IbmInfoSphereDiscoveryImport.CaErwinDataProfiler\nT\n";
            //Result += "IbmRationalDataArchitect\nT\n";
            //Result += "IbmRationalDataArchitectImport.IbmDb2DataWarehouse\nT\n";
            //Result += "IbmRationalRose98Import.MicrosoftVisualStudioModeler\nT\n";
            //Result += "OmgCwmXmiImport.CaErwinDataProfiler\nT\n";
            //Result += "OmgUml2XmiImport.PowerDesignerOom\nT\n";
            //Result += "OmgUml2XmiImport.SparxEA\nT\n";
            //Result += "OmgUmlXmiImport.SparxEA\nT\n";
            //Result += "OmgUmlXmiImport.SybasePowerDesignerOom\nT\n";
            //Result += "OracleDesigner\nT\n";
            //Result += "SybasePowerDesignerCdm06\nT\n";
            //Result += "SybasePowerDesignerCdm07\nT\n";
            //Result += "SybasePowerDesignerLdm15\nT\n";
            //Result += "SybasePowerDesignerPdm07\nT\n";
            //Result += "baseLicense\nDeveloper\n";
            //Result += "updatelevel\n0.0\n";
            //Result += "updates\n1\n";


            //Result += "26\n";
            //Result += "active\nT\n";
            //Result += "beta\n0\n";
            //Result += "exportable\n0\n";
            //Result += "hostpid\n" + pid + "\n";
            //Result += "hostskuid\n" + skuid + "\n";
            //Result += "internaluse\n0\n";
            //Result += "naggy\n0\n";
            //Result += "noncommercial\n0\n";
            //Result += "noncommercial_label\nNo\n";
            //Result += "platform\n1\n";
            //Result += "platform_label\nWindows\n";
            //Result += "product\n2700\n";
            //Result += "productid\n2705\n";
            //Result += "productid_label\nHTML5 Builder\n";         //HTML5 Builder
            //Result += "productsku\n0\n";
            //Result += "productsku_label\nRadPHP\n";               //RadPHP
            //Result += "rndkey\n15687805\n";
            //Result += "serialno\n" + SerialNumber + "\n";
            //Result += "sku\n0\n";
            //Result += "templicense\n0\n";
            //Result += "termtype\n0\n";
            //Result += "termtype_label\nPermanent\n";
            //Result += "title\nHTML5 Builder\n";                    //HTML5 Builder
            //Result += "trial\n0\n";
            //Result += "upgrade\n0\n";
            //Result += "version\n5\n";
            //Result += "1\n";
            //Result += "updatelevel\n0.0\n";

            return Result;
        }


        static uint randomBase = 0x0D138F2E3;
        public static uint RandomNumber()
        {
            randomBase = 0x8088405 * randomBase + 1;
            return randomBase;
        }

        public static uint sub_407334(uint a1)
        {
            ulong tmp = a1 * (ulong)RandomNumber();
            return (uint)(tmp >> 32);
        }

        public static string GetRndKey()
        {
            var v3 = sub_407334(0x20) << 16;
            var v4 = sub_407334(0x20);
            return ((v4 + v3) % 89999999 + 10000000).ToString();
        }
    }
}
